<%@ include file="/WEB-INF/jsp/includes/include.jsp" %>
<%@ attribute name="postings" required="true" type="java.util.Collection" %>

<h2>Your recent biddings:</h2>
<table cellspacing="0" cellpadding="5" border="1">
	<thead>
		<tr>
			<th>NAME</th>
			<th>DESCRIPTION</th>
			<th>RESERVE PRICE</th>
			<th>MAX BID</th>
			<th>YOUR LAST BID</th>
			<th>START DATE</th>
			<th>END DATE</th>
			<th>STATUS</th>
		</tr>
	</thead>
	<tbody>
	<c:forEach var="posting" items="${postings}" varStatus="rowCounter">
        <c:choose>
          <c:when test="${rowCounter.count % 2 == 0}">
            <c:set var="rowStyle" scope="page" value="row1"/>
          </c:when>
          <c:otherwise>
            <c:set var="rowStyle" scope="page" value="row2"/>
          </c:otherwise>
        </c:choose>
		<tr class="${rowStyle}">
			<td><a href="<c:url value="/postings/show/${posting.id}/"/>">${posting.title}</a></td>
			<c:if test="${fn:length(posting.description) > 30}">
			<td><c:out value="${fn:substring(posting.description,0,30)}..." escapeXml="true" /></td>
			</c:if>
			<c:if test="${fn:length(posting.description) <= 30}">
				<td><c:out value="${posting.description}" escapeXml="true" /></td>
			</c:if>
			<td>$${posting.price}</td>
			<td>
				<c:set var="yourCurrentBid" value="0" />
				<c:choose>
			    	<c:when test="${fn:length(posting.bids) > 0}">
			            <c:forEach var="bid" items="${posting.bids}" varStatus="bidCounter">
							<c:if test="${bidCounter.count == 1}">
								<span class="error"><strong>$${bid.price}</strong></span>
							</c:if>
							<c:if test="${bid.user.id eq currentUser.id and yourCurrentBid eq 0}">
								<c:set var="yourCurrentBid" value="${bid.price}" />
							</c:if>
						</c:forEach>
			      	</c:when>
			       	<c:otherwise>
			            not placed yet
			        </c:otherwise>
				</c:choose>
			</td>
			<td>$${yourCurrentBid}</td>
			<td><fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${posting.startDate}" /></td>
			<td><fmt:formatDate type="both" dateStyle="medium" timeStyle="medium"  value="${posting.endDate}" /></td>
			<td>
				<c:if test="${posting.winner.id == null}">
					Ongoing
				</c:if>
				<c:if test="${posting.status eq 2}">
					Closed
				</c:if>
				<c:if test="${posting.winner.id == currentUser.id}">
					<span class="error"><strong>- You&nbsp;won this auction!&nbsp;:)
					Congratulations!</strong></span>
				</c:if>
			</td>
		</tr>
	</c:forEach>
	</tbody>
</table>